//
//  RedBagDetailInfiViewController.m
//  智积分
//
//  Created by Cynic on 2018/7/31.
//  Copyright © 2018年 Cynic. All rights reserved.
//

#import "RedBagDetailInfiViewController.h"
#import "RedBagDetailInfoTableViewCell.h"

static NSString *RedBagDetailInfoTableViewCellIdentifier = @"RedBagDetailInfoTableViewCellIdentifier";

@interface RedBagDetailInfiViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,retain)UITableView *MainView;
@property (nonatomic ,retain)UIView *topView;
@property (nonatomic ,retain)UIButton *header;
@property (nonatomic ,retain)UILabel *name;
@property (nonatomic ,retain)UILabel *centLabel;
@property (nonatomic ,retain)UIButton *totalRedNumBtn;
@property (nonatomic ,retain)UIButton *totalRedGetBtn;
@property (nonatomic ,retain)UIView *headerView;
@property (nonatomic ,retain)UILabel *type;
@property (nonatomic ,retain)UILabel *timelabel;
@property (nonatomic ,copy)NSString *redID;
@property (nonatomic ,retain)NSDictionary *redData;
@property (nonatomic ,retain)NSMutableArray *hisData;
@end

@implementation RedBagDetailInfiViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if (_type) {
        _type.text = @"随机红包";
        _timelabel.text = @"2016-12-15 12:38:08";
    }
}

-(instancetype)initWithID:(NSString *)RedID{
    self = [super init];
    _redID = RedID;
//    [self dataLoad];
    [self hisLoad];
    return self;
}

//-(void)dataLoad{
//    [SVProgressHUD showWithStatus:@"红包信息加载中"];
//    NetWorkModel *netmdoel = [[NetWorkModel alloc] init];
//    [netmdoel getRedBagDetailInfoByID:_redID];
//    WEAKSELF;
//    netmdoel.sendMessage = ^(NSString *result) {
//        if ([result isEqual:@"success"]) {
//            [SVProgressHUD dismiss];
//            weakSelf.redData = [cxsingler.DicData mutableCopy];
//            [weakSelf setRedInfo];
//        }
//        else{
//            [SVProgressHUD showErrorWithStatus:result];
//        }
//    };
//}

-(void)hisLoad{
    [SVProgressHUD showWithStatus:@"历史信息加载中"];
    NetWorkModel *netmodel = [[NetWorkModel alloc] init];
    [netmodel getSingleRedBagGetterHisByID:_redID];
    WEAKSELF;
    netmodel.sendMessage = ^(NSString *result) {
        if ([result isEqual:@"success"]) {
            [SVProgressHUD dismiss];
            weakSelf.redData = [NSDictionary dictionaryWithDictionary:cxsingler.DicData[@"packet"]];
            NSArray *list = [NSArray arrayWithArray:cxsingler.DicData[@"record"]];
            if (list.count > 0) {
                [weakSelf.hisData addObjectsFromArray:list];
            }
            [weakSelf setRedInfo];
            [weakSelf.MainView reloadData];
        }
        else{
            [SVProgressHUD showErrorWithStatus:result];
        }
    };
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = bggrayColor;
    _hisData = [NSMutableArray array];
    _redData = [NSDictionary dictionary];
    self.headerView;
    [self viewLoad];
    
    [self setRedInfo];
   
}

-(void)setRedInfo{
    _name.text = [self Nnll:cxsingler.loginrequest[@"name"] withPlace:@"--"];
    _centLabel.text = [self Nnll:[(NSString *)_redData[@"integral"] MakeIntStr] withPlace:@"--"];
    [_totalRedGetBtn setTitle:[self Nnll:_redData[@"record_count"] withPlace:@"--"] forState:UIControlStateNormal];
    [_totalRedNumBtn setTitle:[self Nnll:_redData[@"count"] withPlace:@"--"] forState:UIControlStateNormal];
}

-(void)viewLoad{
    [self setNavigationViewWithTitle:@"红包详情" WithCanPop:YES WithPopData:@"return" WithPopisString:NO WithHasRightOperation:NO WithrightOperationData:@"" WithRightisString:NO WithNavigationColor:[UIColor ColorWithHex:@"DF4432" withAlpha:1] withBgView:self.view];
    [self.view addSubview:self.topView];
    [self setTop];
    [self.view addSubview:self.MainView];
}

-(UIView *)topView{
    if (!_topView) {
        _topView = [[UIView alloc] initWithFrame:CGRectMake(0, 44 + cxsingler.topdis, cx_wid, 246)];
        _topView.backgroundColor = [UIColor clearColor];
    }
    return _topView;
}

-(UIView *)headerView{
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, cx_wid, 40)];
        _headerView.backgroundColor = [UIColor whiteColor];
        [self.view loadLayerViewBaseView:_headerView withHit:1 withDistype:2 withColor:bggrayColor];
        _type = [[UILabel alloc] initWithFrame:CGRectMake(12, 10, 65, 20)];
        _type.textColor = [UIColor blackColor];
        _type.font = [UIFont systemFontOfSize:14];
        
        _timelabel = [[UILabel alloc] initWithFrame:CGRectMake(cx_wid - 12 - 140, 10, 140, 20)];
        _timelabel.textColor = [UIColor ColorWithHex:@"999999" withAlpha:1];
        _timelabel.font = [UIFont systemFontOfSize:13];
        _timelabel.textAlignment = NSTextAlignmentRight;
        [_headerView addSubview:_type];
        [_headerView addSubview:_timelabel];
    }
    return _headerView;
}

-(void)setTop{
    _header = [[UIButton alloc] initWithFrame:CGRectZero];
    NSString *headerUrl = [NSString stringWithFormat:@"%@",cxsingler.loginrequest[@"logo"]];
    if ([headerUrl containsString:@"gif"]) {
        [_header setImage:[UIImage sd_animatedGIFWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:headerUrl]]] forState:UIControlStateNormal];
    }
    else{
        [_header sd_setBackgroundImageWithURL:[NSURL URLWithString:headerUrl] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"logo1"]];
    }
    [_topView addSubview:_header];
    _header.layer.cornerRadius = 35;
    _header.clipsToBounds = YES;
    _header.backgroundColor = [UIColor lightGrayColor];
    [_header mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(10);
        make.size.mas_equalTo(CGSizeMake(70, 70));
    }];
    
    _name = [[UILabel alloc] initWithFrame:CGRectZero];
    [_topView addSubview:_name];
    [_name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(87);
        make.height.mas_equalTo(15);
        make.width.mas_equalTo(cx_wid - 30);
    }];
    
    _centLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    [_topView addSubview:_centLabel];
    _name.textAlignment = _centLabel.textAlignment = NSTextAlignmentCenter;
    _name.textColor = _centLabel.textColor = [UIColor blackColor];
    [_centLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.top.mas_equalTo(118);
        make.height.mas_equalTo(24);
        make.width.mas_equalTo(cx_wid - 30);
    }];
    
    NSArray *titles = @[@"红包总数",@"领取总数"];
    for (NSInteger index = 0; index < titles.count; index ++) {
        UIButton *PartBtn = [[UIButton alloc] initWithFrame:CGRectZero];
        [_topView addSubview:PartBtn];
        
        PartBtn.namelabel = [[UITextField alloc] initWithFrame:CGRectMake(0, 40, 65, 20)];
        PartBtn.namelabel.textAlignment = NSTextAlignmentCenter;
        PartBtn.namelabel.textColor = [UIColor ColorWithHex:@"9B9B9B" withAlpha:1];
        PartBtn.namelabel.font = [UIFont systemFontOfSize:15];
        [PartBtn setTitleColor:[UIColor ColorWithHex:@"9B9B9B" withAlpha:1] forState:UIControlStateNormal];
        PartBtn.namelabel.text = titles[index];
        [PartBtn addSubview:PartBtn.namelabel];
        
        [PartBtn setTitleEdgeInsets:UIEdgeInsetsMake(-20, 0, 0, 0)];
        if (index == 0) {
            _totalRedNumBtn = PartBtn;
        }
        else{
            _totalRedGetBtn = PartBtn;
        }
        [PartBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        if (index == 0) {
            make.left.mas_equalTo(50);
        }
        else if (index == 1){
            make.right.mas_equalTo(-50);
        }
            make.bottom.mas_equalTo(-30);
            make.size.mas_equalTo(CGSizeMake(65, 60));
            }];
    }
}

-(UITableView *)MainView{
    if (!_MainView) {
        _MainView = [[UITableView alloc] initWithFrame:CGRectMake(0, 44 + cxsingler.topdis + 246, cx_wid, cx_hit - 44 - cxsingler.topdis - cxsingler.bottomdis - 246)];
        _MainView.delegate = self;
        _MainView.dataSource = self;
        _MainView.estimatedRowHeight = 44;
        _MainView.backgroundColor = bggrayColor;
        _MainView.rowHeight = UITableViewAutomaticDimension;
        _MainView.separatorStyle = UITableViewCellSelectionStyleNone;
        [_MainView registerClass:[RedBagDetailInfoTableViewCell class] forCellReuseIdentifier:RedBagDetailInfoTableViewCellIdentifier];
    }
    return _MainView;
}

#pragma mark--uitableview代理start
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _hisData.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return self.headerView;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger row = indexPath.row;
    RedBagDetailInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:RedBagDetailInfoTableViewCellIdentifier];
    NSDictionary *partdic = _hisData[row];
    cell.name.text = partdic[@"nickname"];
    cell.time.text = [self gettimeByInter:[partdic[@"create_time"] integerValue]];
    NSString *headerUrl = [NSString stringWithFormat:@"%@",cxsingler.loginrequest[@"logo"]];
    if ([headerUrl containsString:@"gif"]) {
        [cell.header setImage:[UIImage sd_animatedGIFWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:headerUrl]]] forState:UIControlStateNormal];
    }
    else{
        [cell.header sd_setBackgroundImageWithURL:[NSURL URLWithString:headerUrl] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"logo1"]];
    }
    cell.cents.text = @"1000";
    return cell;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y < 0) {
        [scrollView setContentOffset:CGPointZero];
    }
}
#pragma mark--uitableview代理end


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
